using MediatR;
using Microsoft.AspNetCore.Mvc;
using RBAC.Domain.Dto.Output;
using RBAC.Domain.ProcessManagement;
using RBAC.ErrorCode;
using RBAC.Read.Api.Application.Command.ProcessManagements;

namespace RBAC.Read.Api.Controllers.ProcessManagements
{
    /// <summary>
    /// 工序分类（读端）
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProcessManagementsController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<ProcessManagementsController> _logger;

        public ProcessManagementsController(IMediator mediator, ILogger<ProcessManagementsController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 获取工序分类分页列表
        /// </summary>
        [HttpGet]
        public Task<APIPaging<ProcessClassification>> GetProcessClassificationPage([FromQuery] GetProcessClassificationsPageCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 获取工序列表（联查：分类/部门/负责人）
        /// </summary>
        [HttpGet]
        public Task<APIPaging<ProcessListDto>> GetProcessesPage([FromQuery] GetProcessesPageCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 获取工艺流程列表（分页）
        /// </summary>
        [HttpGet]
        public Task<APIPaging<ProcessFlowListDto>> GetProcessFlowsPage([FromQuery] GetProcessFlowsPageCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 导出工序分类列表到Excel
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> ExportProcessClassifications([FromQuery] ExportProcessClassificationsCommand request)
        {
            var result = await _mediator.Send(request);
            if (result.Code == APIEnums.Success && result.Data != null && result.Data.Length > 0)
            {
                var ts = DateTime.Now.ToString("yyyyMMdd_HHmmss");
                var fileName = $"工序分类_{ts}.xlsx";
                const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                return File(result.Data, contentType, fileName);
            }

            _logger.LogWarning("导出工序分类失败或无数据: {Msg}", result.Msg);
            return BadRequest(result);
        }

        /// <summary>
        /// 导出工序列表到Excel
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> ExportProcesses([FromQuery] ExportProcessesCommand request)
        {
            var result = await _mediator.Send(request);
            if (result.Code == APIEnums.Success && result.Data != null && result.Data.Length > 0)
            {
                var ts = DateTime.Now.ToString("yyyyMMdd_HHmmss");
                var fileName = $"工序_{ts}.xlsx";
                const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                return File(result.Data, contentType, fileName);
            }

            _logger.LogWarning("导出工序失败或无数据: {Msg}", result.Msg);
            return BadRequest(result);
        }

        /// <summary>
        /// 导出工艺流程列表到Excel
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> ExportProcessFlows([FromQuery] ExportProcessFlowsCommand request)
        {
            var result = await _mediator.Send(request);
            if (result.Code == APIEnums.Success && result.Data != null && result.Data.Length > 0)
            {
                var ts = DateTime.Now.ToString("yyyyMMdd_HHmmss");
                var fileName = $"工艺流程_{ts}.xlsx";
                const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                return File(result.Data, contentType, fileName);
            }

            _logger.LogWarning("导出工艺流程失败或无数据: {Msg}", result.Msg);
            return BadRequest(result);
        }
    }
}
